
<template>
  <div>
    <indexHeader @search="searchPost"></indexHeader>
    <van-tabs @click="onClick">
      <van-tab title="推荐" class="infoAll">
        <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
          <!--  这个是全部的帖子列表 -->
          <!-- <div class="selfInfo" v-for="(item, index) in postList" :key="index"> -->
        <!--  这个是全部的帖子列表 -->
        <!-- <div class="selfInfo" v-for="(item, index) in postList" :key="index"> -->
      <!-- <div class="big"> -->
        <div
          class="selfInfo"
          v-for="(item, index) in searchPostList()"
          :key="index"
        >
      
          <!--评论点赞 -->
          <!-- <div
            class="selfInfo"
            v-for="(item, index) in searchPostList()"
            :key="index"
          > -->
            <router-link
              :to="{
                path: '/postDetail',
                query: { postsId: item.postsId },
              }"
              ><div class="infoTitle">
                <span>{{ item.title }}</span>
                <!-- <span class="postTime"></span> -->
              </div>
              <div class="infoTopPic">
                <img :src="item.avatar" alt="" />
              </div>

              <div class="infoName">
                <span style="padding-top: 4px">{{ item.userName }}</span>
                <p class="infoTime" style="margin: 0; padding-top: 4px">
                  <!-- 2021-07-08 10:35 -->{{ item.sendTime }}
                </p>
              </div>
              <!-- 发表的内容名 -->
              <div class="postListContent">
                {{ item.intro }}
              </div>
              <div class="listPic">
                <img :src="item.coverImgUrl" alt="" />
              </div>
            </router-link>
            <!--评论点赞 -->
            <div
              class="postBot"
              style="width: 300px; margin-left: 20px; margin-top: 10px"
            >
              <div class="thumb">
                <li><van-icon name="good-job-o" :badge="item.zan" /></li>

                <li><van-icon name="eye-o" :badge="item.readNum" /></li>
                <!-- <li><van-icon name="chat-o" badge="9" /></li> -->
              </div>
            </div>
          </div>

        </van-pull-refresh>

        <!-- 返回顶部 -->
        <div class="top" @click="goTop" v-if="flag">
          <van-icon name="arrow-up" />
        </div>

      </van-tab>
      <van-tab title="主题" class="itemAll">
        <div class="themeContent">
          <van-grid :border="false" :column-num="4" square>
            <van-grid-item
              class="themeBox"
              v-for="(item, index) in special"
              :key="index"
            >
              <router-link
                :to="{
                  path: '/postList',
                  query: { categoryId: item.categoryId },
                }"
                ><div class="themeBox">
                  <div class="imgBox">
                    <img :src="item.cover" alt="" />
                  </div>
                  <p>{{ item.name }}</p>
                </div></router-link
              >
            </van-grid-item>
          </van-grid>
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>
<script>
import indexHeader from "../components/IndexHeader.vue";
import { categoryList, postList } from "../api/api";
import { Toast } from "vant";
export default {
  name: "indexContent",
  components: {
    indexHeader,
  },
  data() {
    return {
      postList: {},
      special: {},
      categoryId: "",
      value: "",

      isLoading: false,
      newArr: [],

      flag:true,

    };
  },
  methods: {
    onRefresh() {
      setTimeout(() => {
        Toast("刷新成功");
        this.isLoading = false;
        this.count++;
      }, 1000);
      postList().then((res) => {
        console.log(res);
        if (res.code == 0) {
          this.postList = res.rows;
          this.postList.reverse();
        }
      });
    },
    onClick() {},
    // 搜索
    searchPostList() {
      return this.postList.filter((item) => {
        return item.title.includes(this.value);
      });
    },
    searchPost(data) {
      this.value = data;
    },
    goTop(){
      var timer = setInterval(function () {
      if (document.documentElement.scrollTop > 0) {
          document.documentElement.scrollTop -= 100;
      } else {
        clearInterval(timer);
      }

    }, 3) 
    }
  },
  
  created() {
    // 专题列表
    categoryList().then((res) => {
      console.log(res);
      for (let i = 0; i < res.rows.length; i++) {
        if (res.rows[i].categoryId != 4 && res.rows[i].categoryId != 9) {
          this.newArr.push(res.rows[i]);
          if (res.code === 0) {
            this.special = this.newArr;
          }
        }
      }
    });
    // 帖子全部的列表
    postList().then((res) => {
      console.log(res);
      if (res.code == 0) {
        this.postList = res.rows;
        this.postList.reverse();
      }
    });
  
      // if (document.documentElement.scrollTop > 0) {
      //     this.flag=true;
      // } else {
      //   this.flag=false;
      // }
  },
  mounted() {
    
  }
};
</script>

<style scoped lang="less">
.van-pull-refresh {
  background-color: #f4f4f4;
}
.top {
    position: fixed;
    top: 86%;
    right: 3%;
    width: 35px;
    height: 35px;
    // background: url(../images/top.png) no-repeat 0 0;
    background-position: 0 -54px;
    background-color: #f4f4f4;
    text-align: center;
    line-height: 35px;
}
.big {
  background-color: #f4f4f4;
}
.themeContent {
    background-color: #fff;

}
.van-tabs {
    // background-color: #f4f4f4;
    position: relative;
}
.selfInfo {
  margin: auto;
  margin-top: 2%;
  width: 100%;
  background-color: #fff;
}
/deep/.infoAll > .selfInfo:first-child {
  margin-top: -8px;
}
.itemAll {
  background-color: white;
}
.infoTitle {
  margin: 10px 0 10px 20px;
  // height: 34px;
  line-height: 34px;
  text-align: left;
  font-size: 20px;
  color: black;
}
.infoTitle span {
  padding-top: 10px;
}
.infoTopPic {
  float: left;
  margin-top: 10px;
  margin-left: 20px;
  width: 40px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 20px;
  color: black;
}
.infoTopPic > img {
  width: 40px;
  height: 40px;
  border-radius: 20px;
}
.infoName {
  margin-left: 60px;
  margin-top: 20px;
  padding-left: 10px;
  vertical-align: middle;
  font-size: 12px;
  text-align: left;
  color: black;
}
.postListContent {
  margin: 7% 0 2% 8%;
  width: 80%;
  text-align: left;
  color: black;

  /* text-indent: 2em; */
}
.listPic {
  margin: auto;
  width: 86%;
  height: 50%;
}
.listPic > img {
  width: 90%;
}
.thumb {
  display: flex;
  flex-direction: row-reverse;
  list-style: none;
}
.thumb > li:first-child {
  padding-top: 10px;
  padding-right: 12%;
}

.thumb > li:last-child {
  padding-right: 10%;
  padding-top: 10px;
  font-size: 16px;
}
// 帖子样式结束
.van-grid {
  height: 280px;
}
.themeBox {
  margin-bottom: 8%;
}
.themeBox > p {
  display: inline-block;
  margin-top: 6px;
}
.imgBox {
  width: 70px;
  height: 70px;
  // background-color: red;
  border-radius: 40px;
}
.imgBox > img {
  width: 100%;
  height: 100%;
  border-radius: 40px;
}
.themeContent {
  margin: 26px auto 0;
  width: 90%;
}
/deep/.van-grid-item__content {
  text-align: center;
}
/* 标题 */
.item {
  margin-top: 10px;
  width: 100%;
  text-align: center;
}
.item > h3 {
  display: inline-block;
}
.item > h3:nth-of-type(2) {
  margin: 0 20px;
}

/* 帖子主题 */
.themeBox > p {
  font-weight: 560;
  font-size: 14px;
  color: rgb(204, 0, 0);
}
.icon-lvhang:before {
  content: "\e644";
}
::before.icon-lvhang {
  content: "\e644";
}
/deep/.van-tab span {
  font-size: 18px;
}
</style>
